/*
 * Copyright 2009 Millibit Systems.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package jp.millibit.fiber
{
	import flash.events.EventDispatcher;

	[Event(name="complete", type = "jp.millibit.fiber.CallbackEvent")]
	[Event(name="receive", type = "jp.millibit.fiber.CallbackEvent")]

	public dynamic class CallbackQueue extends EventDispatcher
	{
		private var _complete:Boolean;

		private var _values:Array;

		public function CallbackQueue()
		{
			_complete = false;
			_values = [];
		}

		public function get length():uint
		{
			return _values.length;
		}

		public function get complete():Boolean
		{
			return _complete;
		}

		public function dequeue():Object
		{
			return _values.shift();
		}

		public function enqueue(value:Object):void
		{
			_values.push(value);
			dispatchEvent(new CallbackEvent(CallbackEvent.RECEIVE, value));
		}

		internal function setComplete():void
		{
			_complete = true;
			dispatchEvent(new CallbackEvent(CallbackEvent.COMPLETE, null));
		}
	}
}